﻿using CloudDcPc.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CloudDcPc.FuckDlg
{
    /// <summary>
    /// GzEditDlg.xaml 的交互逻辑
    /// </summary>
    public partial class GzEditDlg : Window
    {
        Gz gz = null;
        public GzEditDlg(Gz gz)
        {
            this.gz = gz;
            InitializeComponent();
            main_grid.DataContext = gz;

            List<User> list_approver = App.UserBean.GetUsersByField("type", "3");
            cb_approver.ItemsSource = list_approver;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            BindingExpression bind = txt_sign_name.GetBindingExpression(TextBox.TextProperty);
            bind.UpdateSource();
            bind = txt_department.GetBindingExpression(TextBox.TextProperty);
            bind.UpdateSource();
            bind = txt_phone.GetBindingExpression(TextBox.TextProperty);
            bind.UpdateSource();
            bind = txt_doer.GetBindingExpression(TextBox.TextProperty);
            bind.UpdateSource();
            bind = cb_approver.GetBindingExpression(ComboBox.TextProperty);
            bind.UpdateSource();

            if (txt_sign_name.Text == "" || txt_department.Text == "" || txt_phone.Text == "" || txt_doer.Text == "")
            {
                MessageBox.Show("请将信息填写完整！");
                return;
            }

            if (App.GzBean.HasGzForUpdate(gz.Id,txt_sign_name.Text, txt_department.Text))
            {
                MessageBox.Show("已经存在有相同的签单人和单位的记录！");
                return;
            }

            if (App.GzBean.UpdateGz(gz))
            {


                MessageBox.Show("更新成功！");
                this.Close();
            }
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            string messageBoxText = "确认删除该条挂帐单位信息吗？";
            string caption = "删除信息";
            MessageBoxButton button = MessageBoxButton.YesNo;
            MessageBoxImage icon = MessageBoxImage.Warning;
            MessageBoxResult result = MessageBox.Show(messageBoxText, caption, button, icon);
            if (result == MessageBoxResult.Yes)
            {
                if (App.GzBean.DelGzById(gz.Id))
                {
                    MessageBox.Show("删除成功！");
                    this.DialogResult = true;
                    this.Close();
                }
                else
                {
                    MessageBox.Show("删除失败！");
                }
            }
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
